package com.xr.core.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xr.core.system.common.base.MyPage;
import com.xr.core.system.common.base.Result;
import com.xr.core.system.common.groups.Save;
import com.xr.core.system.common.groups.Update;
import com.xr.core.system.entity.exceptionLogModule.ExceptionLog;
import com.xr.core.system.service.ExceptionLogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 异常日志表(ExceptionLog)表控制层
 */
@Validated
@RestController
@RequestMapping("/api/v1/exceptionLog")
public class ExceptionLogController {

    /**
     * 服务对象
     */
    @Resource
    private ExceptionLogService exceptionLogService;

    /**
     * 查询所有数据
     *
     * @param exceptionLog 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryAll")
    public Result<List<ExceptionLog>> queryAll(ExceptionLog exceptionLog) {
        return Result.success(exceptionLogService.list(new QueryWrapper<>(exceptionLog)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page         分页对象
     * @param exceptionLog 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryByPage")
    public Result<MyPage<ExceptionLog>> queryByPage(MyPage<ExceptionLog> page, ExceptionLog exceptionLog) {
        return Result.success(exceptionLogService.page(page, new QueryWrapper<>(exceptionLog)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryOne/{id}")
    public Result<ExceptionLog> selectOne(@PathVariable Serializable id) {
        return Result.success(exceptionLogService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param exceptionLog 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public Result<Object> insert(@Validated(Save.class) @RequestBody ExceptionLog exceptionLog) {
        return Result.isSuccess(exceptionLogService.save(exceptionLog));
    }

    /**
     * 修改数据
     *
     * @param exceptionLog 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public Result<Object> update(@Validated(Update.class) @RequestBody ExceptionLog exceptionLog) {
        return Result.isSuccess(exceptionLogService.updateById(exceptionLog));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        ExceptionLog exceptionLog = new ExceptionLog();
        exceptionLog.setId(id);
        return Result.isSuccess(exceptionLogService.removeById(exceptionLog));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键集合
     * @return 删除结果
     */
    @DeleteMapping("/batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(exceptionLogService.batchDelete(idList, new ExceptionLog()));
    }

}

